package org.csu.mypetstore.web.servlets;

import com.github.wxpay.sdk.WechatOrder;
import org.csu.mypetstore.domain.Order;
import org.csu.mypetstore.service.OrderService;
import org.csu.mypetstore.service.impl.OrderServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class CreateOrderServlet extends HttpServlet {
    private static final String PAYMENT = "/WEB-INF/jsp/order/Payment.jsp";

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        if(req.getParameter("confirmed") != null && req.getParameter("confirmed").equals("true")) {
            Order order = (Order)req.getSession().getAttribute("order");

            OrderService orderService = new OrderServiceImpl();
            orderService.createOrder(order);

            //下订单生成二维码链接
            WechatOrder wechatOrder = new WechatOrder(order.getUsername());
            //测试用1分钱******************************
            wechatOrder.order(order.getTotalPrice().doubleValue() * 0.1);

            req.getSession().setAttribute("orderId", order.getOrderId());
            req.getSession().setAttribute("wechatOrder", wechatOrder);
            req.getRequestDispatcher(PAYMENT).forward(req, resp);//转去微信支付页面
        }
    }
}
